<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch" label-width="68px">
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="Plus"
          @click="handleAdd"
          v-hasPermi="['processcontent:processcontent:add']"
        >新增</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="Edit"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['processcontent:processcontent:edit']"
        >修改</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="Delete"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['processcontent:processcontent:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="Download"
          @click="handleExport"
          v-hasPermi="['processcontent:processcontent:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar v-model:showSearch="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="processcontentList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <!-- <el-table-column label="内容ID" align="center" prop="contentId" />
      <el-table-column label="工序ID" align="center" prop="processId" /> -->
      <el-table-column label="顺序编号" align="center" prop="orderNum" />
      <el-table-column label="内容说明" align="center" prop="contentText" />
      <el-table-column label="辅助设备" align="center" prop="device" />
      <el-table-column label="辅助材料" align="center" prop="material" />
      <el-table-column label="材料URL" align="center" prop="docUrl" />
      <el-table-column label="备注" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template #default="scope">
          <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)" v-hasPermi="['processcontent:processcontent:edit']">修改</el-button>
          <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)" v-hasPermi="['processcontent:processcontent:remove']">删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    
    <pagination
      v-show="total>0"
      :total="total"
      v-model:page="queryParams.pageNum"
      v-model:limit="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改生产工序内容对话框 -->
    <el-dialog :title="title" v-model="open" width="500px" append-to-body>
      <el-form ref="processcontentRef" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="工序ID" prop="processId">
          <el-input v-model="form.processId" placeholder="请输入工序ID" readonly/>
        </el-form-item>
        <el-form-item label="顺序编号" prop="orderNum">
          <el-input v-model="form.orderNum" placeholder="请输入顺序编号" />
        </el-form-item>
        <el-form-item label="内容说明" prop="contentText">
          <el-input v-model="form.contentText" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="辅助设备" prop="device">
          <el-input v-model="form.device" placeholder="请输入辅助设备" />
        </el-form-item>
        <el-form-item label="辅助材料" prop="material">
          <el-input v-model="form.material" placeholder="请输入辅助材料" />
        </el-form-item>
        <el-form-item label="材料URL" prop="docUrl">
          <el-input v-model="form.docUrl" placeholder="请输入材料URL" />
        </el-form-item>
        <el-form-item label="备注" prop="remark">
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" />
        </el-form-item>
      </el-form>
      <template #footer>
        <div class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </template>
    </el-dialog>
  </div>
</template>

<script setup name="Processcontent">
import { listProcesscontent, getProcesscontent, delProcesscontent, addProcesscontent, updateProcesscontent } from "@/api/processcontent/processcontent"
import { watch, onMounted } from 'vue';
const { proxy } = getCurrentInstance()
// 1. 定义接收父组件传递的props（关键修改）
const props = defineProps({
  // 父组件传递的工序ID（kebab-case转为camelCase）
  currentProcessId: {
    type: [Number, String], // 支持数字或字符串类型的ID
    required: true, // 必须传递该参数
    default: null
  }
});
const processcontentList = ref([])
const open = ref(false)
const loading = ref(true)
const showSearch = ref(true)
const ids = ref([])
const single = ref(true)
const multiple = ref(true)
const total = ref(0)
const title = ref("")

const data = reactive({
  form: {},
  queryParams: {
    pageNum: 1,
    pageSize: 10,
    processId: null,
    orderNum: null,
    contentText: null,
    device: null,
    material: null,
    docUrl: null,
  },
  rules: {
  }
})

const { queryParams, form, rules } = toRefs(data)
// 新增：标记组件是否挂载完成
const isMounted = ref(false);

// 组件挂载完成后标记
onMounted(() => {
  isMounted.value = true;
});

// 优化watch：仅当组件挂载完成且currentRouteId有值时才加载
watch(
  () => props.currentprocessId,
  (newVal) => {
    console.log("父组件传递的工序ID：", newVal); // 切换工序时，日志应同步变化
    if (isMounted.value && newVal) { // 新增isMounted判断
      getList();
    }
  },
  { immediate: true }
);

/** 查询生产工序内容列表（优化参数传递） */
function getList() {
  loading.value = true;
  // 确保查询参数携带当前工序ID（来自父组件）
  const params = {
    ...queryParams.value,
    processId: props.currentProcessId // 强制使用父组件传递的工序ID
  };
  listProcesscontent(params).then(response => {
    processcontentList.value = response.rows;
    total.value = response.total;
    loading.value = false;
  }).catch(() => {
    loading.value = false;
  });
}

// 取消按钮
function cancel() {
  open.value = false
  reset()
}

// 表单重置
function reset() {
  form.value = {
    contentId: null,
    processId: props.currentProcessId, // 重置时仍关联当前工序ID
    orderNum: null,
    contentText: null,
    device: null,
    material: null,
    docUrl: null,
    remark: null,
    attr1: null,
    attr2: null,
    attr3: null,
    attr4: null,
    createBy: null,
    createTime: null,
    updateBy: null,
    updateTime: null
  }
  proxy.resetForm("processcontentRef")
}



// 多选框选中数据
function handleSelectionChange(selection) {
  ids.value = selection.map(item => item.contentId)
  single.value = selection.length != 1
  multiple.value = !selection.length
}

/** 新增按钮操作 */
function handleAdd() {
  reset()
  open.value = true
  title.value = "添加生产工序内容"
}

/** 修改按钮操作 */
function handleUpdate(row) {
  reset()
  const _contentId = row.contentId || ids.value
  getProcesscontent(_contentId).then(response => {
    form.value = response.data
    open.value = true
    title.value = "修改生产工序内容"
  })
}

/** 提交按钮 */
function submitForm() {
  proxy.$refs["processcontentRef"].validate(valid => {
    if (valid) {
      if (form.value.contentId != null) {
        updateProcesscontent(form.value).then(response => {
          proxy.$modal.msgSuccess("修改成功")
          open.value = false
          getList()
        })
      } else {
        addProcesscontent(form.value).then(response => {
          proxy.$modal.msgSuccess("新增成功")
          open.value = false
          getList()
        })
      }
    }
  })
}

/** 删除按钮操作 */
function handleDelete(row) {
  const _contentIds = row.contentId || ids.value
  proxy.$modal.confirm('是否确认删除生产工序内容编号为"' + _contentIds + '"的数据项？').then(function() {
    return delProcesscontent(_contentIds)
  }).then(() => {
    getList()
    proxy.$modal.msgSuccess("删除成功")
  }).catch(() => {})
}

/** 导出按钮操作 */
function handleExport() {
  proxy.download('processcontent/processcontent/export', {
    ...queryParams.value
  }, `processcontent_${new Date().getTime()}.xlsx`)
}

getList()
</script>
